<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: 20px auto;
            overflow: hidden;
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        header {
            background: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }

        nav {
            display: flex;
            justify-content: center;
            background: #555;
            padding: 10px 0;
        }

        nav a {
            color: white;
            text-decoration: none;
            padding: 0 15px;
        }

        nav a:hover {
            background: #444;
        }

        .file-list {
            list-style: none;
            padding: 0;
        }

        .file-list li {
            background: #fff;
            border: 1px solid #ddd;
            margin-bottom: 10px;
            padding: 10px;
        }

        .file-list li:hover {
            background: #f9f9f9;
        }

        .file-name {
            font-weight: bold;
        }

        .file-size,
        .file-type,
        .file-actions {
            font-size: 0.9em;
            color: #666;
        }

        .file-actions a {
            padding: 5px 10px;
            background: #333;
            color: #fff;
            border: none;
            cursor: pointer;
            text-decoration: none;
            margin-right: 5px;
            display: inline-block;
        }

        .file-actions a:hover {
            background: #444;
        }

        .upload-form {
            margin-top: 20px;
        }

        footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: relative;
            bottom: 0;
            width: 100%;
        }
    </style>
    
</head>

<body>
    <header>
        <h1>文件管理</h1>
    </header>
    <nav>
        <a href="index.html">返回首页</a>
    </nav>
    <div class="container">
        <h2>文件列表</h2>
        <ul class="file-list">
            <!-- 复制上面的 <li> 来创建更多的文件条目 -->
        </ul>

        <h2>上传文件</h2>
        <form>
            <input type="file" accept="image/jpeg,image/png" class="file" name="file">
            <input type="button" class="upload" value="上传文件">
        </form>
    </div>
    <footer>
        <p>版权所有 © 2024 新闻网站</p>
    </footer>
</body>
<script src="./js/jquery-3.7.1.min.js"></script>
<script>
    $(".upload").click(function () {
        // 手动构建表单
        let formData = new FormData()
        // 添加上传文件的二进制
        let file = $(".file")[0].files[0]
        formData.append("file", file)
        $.ajax({
            url: "http://www.dlrb.com:8082/upload.php",
            type: "POST",
            xhrFields: {
                withCredentials: true
            },
            data: formData,
            processData: false,
            contentType: false,
            dataType: 'json',
            success: (resp) => {
                console.log(resp);
                if (resp.code == 200) {
                    alert(resp.msg)
                    getFileList()
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    })
    getFileList()
    function getFileList() {
        $.ajax({
            url: "http://www.dlrb.com:8082/filelist.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    let content = ""
                    for (let item of resp.data) {
                        content += `<li>
                            <div class='file-name'><img src='${item.fileurl}' width="50" alt="美女图片"></div>
                            <div class='file-size'>${item.filesize}</div>
                            <div class='file-type'>${item.filetype}</div>
                            <div class='file-actions'>
                                <a href='${item.fileurl}' download class='download'>下载</a>
                                <a href='javascript:void(0);' onclick=copyUrl('${item.fileurl}') class='download'>复制地址</a>
                                <a href='javascript:void(0);' onclick=deleteByName('${item.fileurl}') class='delete'>删除</a>
                            </div>
                        </li>`
                    }
                    $(".file-list").html(content);
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }

    function deleteByName(url) {
        $.ajax({
            url: "http://www.dlrb.com:8082/deleteFile.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            data: {
                url: url
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    alert(resp.msg)
                    getFileList()
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }
    async function copyUrl(text) {
        // 传统方法：使用 textarea 和 execCommand
        const textArea = document.createElement('textarea');
        textArea.value = text;
        textArea.style.position = 'fixed';
        textArea.style.opacity = '0';
        document.body.appendChild(textArea);
        textArea.select();

        try {
            const successful = document.execCommand('copy');
            return successful;
        } catch (err) {
            console.error('传统复制方法也失败:', err);
            return false;
        } finally {
            document.body.removeChild(textArea);
        }
    }
</script>

</html>